package com.zmn.plat.admin.controller.message.group;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.common.dto.module.ModuleDIO;
import com.zmn.mcc.common.dto.module.ModuleDRO;
import com.zmn.mcc.dubbo.interfaces.module.ModuleListRemoteService;
import com.zmn.plat.admin.controller.message.MsgTemplateBaseController;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.MsgTemplateDict;
import com.zmn.plat.model.entity.message.MsgTemplateGroup;
import com.zmn.plat.model.entity.message.MsgTemplateGroupQuery;
import com.zmn.plat.model.entity.message.MsgTemplateGroupVO;

/**
 * 描述: 消息管理模板组查询Controller
 *
 * @auther tanghongjun
 * @since 2019/10/21 16:36
 */
@Controller
@RequestMapping("/msg/temp/group/")
public class MsgTemplateGroupListController extends MsgTemplateBaseController {
    private Logger logger = LoggerFactory.getLogger(MsgTemplateGroupListController.class);

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    protected ModuleListRemoteService moduleService;

    /**
     * 描述: 消息模板组列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     */
    @RequestMapping(value = "list")
    @RequiresPermissions("/msg/temp/group/list.action")
    public ModelAndView list(@ModelAttribute MsgTemplateGroupQuery query) {

        ModelAndView mav = new ModelAndView("message/group/list");
        List<MsgTemplateGroup> msgTemplateGroupList = msgTemplateGroupService.listPageByQuery(query);

        // 查询所属模块信息
        ModuleDIO moduleQuery = new ModuleDIO();
        moduleQuery.setStatus(GlobalConsts.YES);
        ResponseDTO<List<ModuleDRO>> listResponseDTO = moduleService.listByQuery(moduleQuery);

        List<ModuleDRO> modules;
        if (listResponseDTO.isSuccess()) {
            modules = listResponseDTO.getData();
        } else {
            modules = Collections.emptyList();
        }

        List<DictModel> platTypeList = GlobalDict.getPlatTypeList();
        Map<Integer, String> platMap = platTypeList.stream().collect(Collectors.toMap(DictModel::getKey, DictModel::getValue));
        Map<Integer, String> moduleMap = modules.stream().collect(Collectors.toMap(ModuleDRO::getModuleId, ModuleDRO::getModuleName));
        List<MsgTemplateGroupVO> items = msgTemplateGroupList.stream().map(msgTemplateGroup -> {
            MsgTemplateGroupVO msgTemplateGroupVO = new MsgTemplateGroupVO();
            BeanUtils.copyProperties(msgTemplateGroup, msgTemplateGroupVO);
            msgTemplateGroupVO.setPlatName(platMap.get(msgTemplateGroup.getPlat()));
            msgTemplateGroupVO.setModuleName(moduleMap.get(msgTemplateGroup.getModule()));
            return msgTemplateGroupVO;
        }).collect(Collectors.toList());

        mav.addObject("modules", modules);
        mav.addObject("vo", query);
        mav.addObject("statuses", MsgTemplateDict.getStatuslist());
        mav.addObject("items", items);
        return mav;
    }

    /**
     * 描述: 模板组添加修改页面跳转
     *
     * @param groupId
     *            模板组id
     * @return modelAndView
     * @author tanghongjun
     * @since 2019年10月22日10:41:50
     */
    @RequestMapping(value = "addEditView")
    @RequiresPermissions("/msg/temp/group/addEditView.action")
    public ModelAndView addEditView(@RequestParam(value = "groupId", defaultValue = "0") Integer groupId) {
        ModelAndView mav = new ModelAndView("message/group/detail");
        MsgTemplateGroup msgTemplateGroup =
            groupId > GlobalConsts.NONE ? msgTemplateGroupService.findByKey(groupId) : new MsgTemplateGroup();
        mav.addObject("msgTemplateGroup", msgTemplateGroup);

        // 查询所属模块信息
        ModuleDIO moduleQuery = new ModuleDIO();
        moduleQuery.setStatus(GlobalConsts.YES);
        ResponseDTO<List<ModuleDRO>> listResponseDTO = moduleService.listByQuery(moduleQuery);

        List<ModuleDRO> modules;
        if (listResponseDTO.isSuccess()) {
            modules = listResponseDTO.getData();
        } else {
            modules = Collections.emptyList();
        }

        mav.addObject("modules", modules);
        mav.addObject("platTypeList", GlobalDict.getPlatTypeList());

        return mav;
    }
}
